package demo.practice.leetcode_msjd;

public class Msjd0106 {

    public static void main(String[] args) {
        Msjd0106 msjd0106= new Msjd0106();
        msjd0106.compressString(null);
    }

    public String compressString(String S) {
        if("".equals(S))
            return S;
        char befor=S.charAt(0);
        int  befortime=1;

        StringBuffer sb= new StringBuffer();
        for(int i=1;i<S.length();i++){
            char c = S.charAt(i);
            if(befor!=S.charAt(i) ){
                sb.append(befor);
                sb.append(befortime);
                befor=c;
                befortime=1;
            }else{
                befortime++;
            }
        }
        sb.append(befor);
        sb.append(befortime);


        String compress = sb.toString();
        return  compress.length()<S.length()? compress :S;
    }
}
